// pages/instdetail/index.js
import urls from '../../utils/urls.js'
import loginFn from '../../utils/login.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		alldetail: '',
		showwhat: '', //类型
		imgalist: [],
		qrcodeimg: '',
		from: '',
		title: '要培训'
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		var that = this
		var pages = getCurrentPages()
		var prepages = pages[pages.length - 2]
		var alldetail = prepages.data.alldetail;
		console.log('alldetail', alldetail)
		let title = '要培训';
		if (options.type == 'teacher') {

			var date = new Date;
			var year = date.getFullYear();
			alldetail.bornYear = parseInt(year) - parseInt(alldetail.bornYear)
			for (var j = 0; j < app.globalData.allselect.courseMain.length; j++) {

				if (alldetail.courseMainId == app.globalData.allselect.courseMain[j].courseMainId) {
					for (var h = 0; h < app.globalData.allselect.courseMain[j].sub.length; h++) {
						if (app.globalData.allselect.courseMain[j].sub[h].courseSubId == alldetail.courseSub)
							alldetail['major'] = app.globalData.allselect.courseMain[j].sub[h].courseSub
					}
				};
			}
			console.log('教师')
			console.log(alldetail)
			this.setData({
				showwhat: 'teacher',
				alldetail: alldetail,
				from: options.from ? options.from : ''
			})

			let page = 'pages/teacherdetail/index';
			let scene = 't=' + alldetail.teacherId;
			title = '这个老师挺靠谱，教学质量也挺高，你了解一下。';
			if(options.from && options.from == 'main') {
				page = 'pages/teacherpage/teacherpage';
				scene = 't='+alldetail.teacherId;
				title = '欢迎进入我的个人主页';
			}

			wx.request({
				url: urls.qrcode,
				method: 'POST',
				data: {
					'page': page,
					'width': 91,
					'scene': scene
				},
				success: function(res) {
					if (res.data.code == 0) {
						var imgalist = []
						imgalist[0] = urls.apiUrl + res.data.data.file
						that.setData({
							qrcodeimg: urls.apiUrl + res.data.data.file,
							imgalist: imgalist
						})
					}
				}

			})




		} else if (options.type == 'school') {
			alldetail.uploadImg = alldetail.logoUrl
			this.setData({
				showwhat: 'school',
				alldetail: alldetail,
				from: options.from ? options.from : ''
			})

			let page = 'pages/instdetail/index';
			let scene = 's=' + alldetail.schoolId;
			title = '都说这个学校好，你要不跟我一起去看看？';
			if(options.from && options.from == 'main') {
				page = 'pages/institufirst/index';
				title = alldetail.schoolName+'欢迎您！';
			}

			if(options.from && options.from == 'admin') {
				let userinfo = loginFn();
				page = 'pages/institucharge/institucharge';
				scene = 'f='+userinfo.loginUid;
				title = '你快用这个招生神器！效果真好！';
			}

			wx.request({
				url: urls.qrcode,
				method: 'POST',
				data: {
					'page': page,
					'width': 91,
					'scene': scene
				},
				success: function(res) {
					if (res.data.code == 0) {
						var imgalist = []
						imgalist[0] = urls.apiUrl + res.data.data.file
						that.setData({
							qrcodeimg: urls.apiUrl + res.data.data.file,
							imgalist: imgalist
						})
					}
				}

			})
		} else if (options.type == 'class') {
			title = '这个课程不错，你快看看，跟我一起报个名？';
			this.setData({
				showwhat: 'class',
				alldetail: alldetail
			})
			wx.request({
				url: urls.qrcode,
				method: 'POST',
				data: {
					'page': 'pages/classdetail/index',
					'width': 91,
					'scene': 'courseId=' + alldetail.courseId
				},
				success: function(res) {
					if (res.data.code == 0) {
						var imgalist = []
						imgalist[0] = urls.apiUrl + res.data.data.file
						that.setData({
							qrcodeimg: urls.apiUrl + res.data.data.file,
							imgalist: imgalist
						})
					}
				}

			})
		}
		console.log('进来的类型')
		console.log(options)
		console.log(this.data.alldetail)
		this.setData({
			title: title
		});

	},
	previewImage: function(e) {
		wx.previewImage({
			urls: this.data.imgalist // 需要预览的图片http链接列表 
		});


	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	saveFn: function() {
		if (this.data.showwhat == 'class') {
			wx.saveImageToPhotosAlbum({
				filePath: urls.apiUrl+'qr,shareCourse?courseId=' + this.data.alldetail.courseId
			})
		} else if (this.data.showwhat == 'teacher') {
			wx.saveImageToPhotosAlbum({
				filePath: urls.apiUrl+'qr,shareCourse?teacherId=' + this.data.alldetail.teacherId
			})
		} else if (this.data.showwhat == 'school') {
			wx.saveImageToPhotosAlbum({
				filePath: urls.apiUrl+'qr,shareCourse?schoolId=' + this.data.alldetail.schoolId
			})
		}

	},
	//点击保存图片
	save() {
		let that = this
		//若二维码未加载完毕，加个动画提高用户体验
		wx.showToast({
			icon: 'loading',
			title: '正在保存图片',
			duration: 1000
		})
		//判断用户是否授权"保存到相册"
		wx.getSetting({
			success(res) {
				//没有权限，发起授权
				if (!res.authSetting['scope.writePhotosAlbum']) {
					wx.authorize({
						scope: 'scope.writePhotosAlbum',
						success() { //用户允许授权，保存图片到相册
							that.savePhoto();
						},
						fail() { //用户点击拒绝授权，跳转到设置页，引导用户授权
							wx.openSetting({
								success() {
									wx.authorize({
										scope: 'scope.writePhotosAlbum',
										success() {
											that.savePhoto();
										}
									})
								}
							})
						}
					})
				} else { //用户已授权，保存到相册
					that.savePhoto()
				}
			}
		})
	},
	//保存图片到相册，提示保存成功
	savePhoto() {
		let that = this
		var filePath = ''
		var loginInfo = loginFn()
		var pathoptions = '&loginKey=' + loginInfo['loginKey']
		pathoptions += '&loginTime=' + loginInfo['loginTime']
		pathoptions += '&loginUid=' + loginInfo['loginUid'];

		let url = urls.apiUrl+'qr,share?';
		if (this.data.showwhat == 'class') {

			filePath = url+'c=' + this.data.alldetail.courseId + pathoptions

		} else if (this.data.showwhat == 'teacher') {
			filePath = url+'t=' + this.data.alldetail.teacherId + pathoptions+'&page=';

			let params = 'pages/teacherdetail/index';
			if(this.data.from == 'main') {
				params = 'pages/teacherpage/teacherpage&a=isTeacher/1';
			}

			filePath += params;
		} else if (this.data.showwhat == 'school') {

			filePath = url+'s=' + this.data.alldetail.schoolId + pathoptions+'&page=';

			let params = 'pages/instdetail/index';
			if(this.data.from == 'main') {
				params = 'pages/institufirst/index&a=isAdmin/1';
			}

			if(this.data.from == 'admin') {
				params = 'pages/institucharge/institucharge';
			}

			filePath += params;

		}
		filePath += '&title='+this.data.title;
		console.log("地址")
		console.log(filePath)
		console.log(pathoptions)
		wx.downloadFile({
			url: filePath,
			success: function(res) {
				console.log('下载')
				console.log(res)
				wx.saveImageToPhotosAlbum({

					filePath: res.tempFilePath,
					success(res) {
						wx.showToast({
							title: '保存成功',
							icon: "success",
							duration: 1000
						})
					}
				})
			}
		})
	}
})
